package com.example.module_happy

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.lifecycleScope
import com.example.lib_base.BaseFragment
import com.example.module_happy.databinding.FragmentHappyBinding
import com.example.module_happy.entity.MultiTypeEntity
import com.example.module_happy.entity.SimpleVideoEntity
import com.example.module_happy.intent.HappyIntent
import com.example.module_happy.state.HappyState
import com.example.module_happy.viewmodel.HappyViewModel
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch


class HappyFragment : BaseFragment<FragmentHappyBinding>() {
    private val viewModel by lazy{HappyViewModel()}
    private val adapter by lazy{HappyAdapter<MultiTypeEntity>({R.layout.first_item to BR.first})}
    private val adapter2 by lazy{HappyAdapter<SimpleVideoEntity>({R.layout.second_item to BR.second})}
    override fun initData() {
    lifecycleScope.launch {
        viewModel.observerState.collect{
            when(it){
                is HappyState.GetTypeSuccess->{
                    adapter.clear()
                    adapter+=it.list
                }
                is HappyState.GetSimpleSuccess->{
                    adapter2.clear()
                    adapter2+=it.list
                }
                else->{

                }
            }
        }
    }
    }

    override fun initView() {
        binding.firstRec.adapter=adapter
        binding.secondRec.adapter=adapter2
    lifecycleScope.launch {
        viewModel.channel.send(HappyIntent.GetMultiType)
        viewModel.channel.send(HappyIntent.GetSimpleVideo(1,10))
    }
    }

    override fun getLayoutId(): Int =R.layout.fragment_happy

}